package com.xx.eshop.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.miemiedev.mybatis.paginator.domain.Order;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.xx.eshop.dao.CategoryDao;
import com.xx.eshop.dao.ProductAttrValueDao;
import com.xx.eshop.dao.ProductDao;
import com.xx.eshop.dao.ProductImageDao;
import com.xx.eshop.entity.Product;
import com.xx.eshop.entity.ProductAttrValue;
import com.xx.eshop.entity.ProductImage;
import com.xx.eshop.service.CategoryService;
import com.xx.eshop.service.ProductService;

@Service
@Transactional
public class ProductServiceImpl implements ProductService {

	@Autowired
	private CategoryDao categoryDao;
	
	@Autowired
	private ProductDao productDao;

	@Autowired
	private ProductImageDao productImageDao;

	@Autowired
	private ProductAttrValueDao productAttrValueDao;

	

	@Override
	public Product findById(int id) {
		return productDao.findById(id);
	}

	@Override
	public Map<String,Object> findByPage(String name, Double minPrice, Double maxPrice, Boolean onSale, 
			int page, int rows, String sort, String order) {
		Map<String, Object> conditions = new HashMap<>();
		conditions.put("name", name);
		conditions.put("minPrice", minPrice);
		conditions.put("maxPrice", maxPrice);
		conditions.put("onSale", onSale);
		PageBounds pb;
		if(sort!=null && order != null){
			pb = new PageBounds(page, rows, Order.formString(sort+"."+order));
		} else {
			pb = new PageBounds(page, rows, Order.formString("id.asc"));
		}
		PageList<Product> list = productDao.findByPage(conditions , pb);
		Map<String, Object> result = new HashMap<>();
		result.put("total", list.getPaginator().getTotalCount());
		result.put("rows", list);
		return result;
	}

	@Override
	public void save(Product product, List<String> images,List<String> attr_names, List<String> attr_values) {
		// 1. 保存商品
		productDao.save(product);		
		int pid = product.getId();
		
		// 2. 保存商品图片
		for(String url : images) {
			//System.out.println("images url = "+url);
			ProductImage img = new ProductImage();
			img.setUrl(url);
			img.setProduct_id(pid);
			//System.out.println("ProductServiceImpl 中 保存商品图片时 product_id "+pid);
			//System.out.println("ProductServiceImpl 中 保存商品图片时 url "+url);
			productImageDao.save(img);
		}
		
		// 3. 保存商品属性
		for(int i = 0; i < attr_names.size(); i++) {
			ProductAttrValue pav = new ProductAttrValue();
			pav.setAttr_name(attr_names.get(i)); // 属性名
			pav.setValue(attr_values.get(i)); // 属性值 
			pav.setProduct_id(pid);
			pav.setCategory_id(product.getCategory_id());
			System.out.println("ProductServiceImpl 中 保存商品属性时 product_id "+pid);
			System.out.println("ProductServiceImpl 中 保存商品属性时 category_id "+product.getCategory_id());
			productAttrValueDao.save(pav);
		}
		
	}

	@Override
	public void update(Product product, List<String> images, List<String> attr_names,
			List<String> attr_values) {
		
		// 1. 保存商品
		productDao.update(product);		
		int pid = product.getId();
		
		
		
		productImageDao.delete(pid);	
		// 2. 保存商品图片
		for(String url : images) {
			System.out.println("images url = "+url);	
			ProductImage img = new ProductImage();
			img.setUrl(url);
			img.setProduct_id(pid);
			System.out.println("ProductServiceImpl 中 修改商品图片时 product_id "+pid);
			System.out.println("ProductServiceImpl 中 修改商品图片时 url "+url);
			productImageDao.save(img);
		}
			
		productAttrValueDao.delete(pid);
		// 3. 保存商品属性
		for(int i = 0; i < attr_names.size(); i++) {
			ProductAttrValue pav = new ProductAttrValue();
			pav.setAttr_name(attr_names.get(i)); // 属性名
			pav.setValue(attr_values.get(i)); // 属性值 
			pav.setProduct_id(pid);
			pav.setCategory_id(product.getCategory_id());
			System.out.println("--- 修改商品属性时 ProductAttrValue ---"+pav.toString());
			System.out.println("ProductServiceImpl 中 修改商品属性时 product_id "+pid);
			System.out.println("ProductServiceImpl 中 修改商品属性时 category_id "+product.getCategory_id());
			productAttrValueDao.save(pav);
		}	
		
	}

	@Override
	public void delete(int product_id) {
		productAttrValueDao.delete(product_id);
		productImageDao.delete(product_id);
		productDao.delete(product_id);
	}

}
